package com.zb.validation;

import com.zb.annotation.IntegerPattern;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @Author WEN
 * @Date 2023/8/2 0002
 **/
public class IntegerPatternValidator implements ConstraintValidator<IntegerPattern, Integer> {


    /**
     * 数值正则表示式：正数、负数、和小数
     */
    private String regexp;

    @Override
    public void initialize(IntegerPattern constraintAnnotation) {
       this.regexp = constraintAnnotation.regexp();
    }

    @Override
    public boolean isValid(Integer val, ConstraintValidatorContext constraintValidatorContext) {
        if (Objects.isNull(val)) {
            return true;
        }
        return Pattern.matches(this.regexp, val.toString());
    }
}
